<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="entryName">
        <el-input
          v-model="queryParams.entryName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="申请部门" prop="depId">
        <el-input
          v-model="queryParams.depId"
          placeholder="请输入申请部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="申请部门" prop="depId">
<el-select v-model="queryParams.depId" placeholder="请选择">
            <el-option
              v-for="item in department"
              :key="item.depId"
              :label="item.depName"
              :value="item.depId">
            </el-option>
          </el-select>

      </el-form-item>
      <!-- <el-form-item label="申请金额" prop="applicationAmount">
        <el-input
          v-model="queryParams.applicationAmount"
          placeholder="请输入申请金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="applicationDate">
        <el-date-picker clearable
          v-model="queryParams.applicationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="负责人" prop="empId">
        <el-input
          v-model="queryParams.empId"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

  <el-form-item label="负责人" prop="empId">
<el-select v-model="queryParams.empId" placeholder="请选择">
            <el-option
              v-for="item in employee"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId">
            </el-option>
          </el-select>

      </el-form-item>

      <!-- <el-form-item label="拨款人" prop="appropriator">
        <el-input
          v-model="queryParams.appropriator"
          placeholder="请输入拨款人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="拨款日期" prop="appropriationDate">
        <el-date-picker clearable
          v-model="queryParams.appropriationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择拨款日期">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="版本号" prop="version">
        <el-input
          v-model="queryParams.version"
          placeholder="请输入版本号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini" round
          @click="handleAdd"
          v-hasPermi="['official:money:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini" round
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['official:money:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini" round
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['official:money:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini" round
          @click="handleExport"
          v-hasPermi="['official:money:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moneyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="spendMoneyId" />
      <el-table-column label="项目名称" align="center" prop="entryName" />
      <el-table-column label="申请部门" align="center" prop="depName" />
      <el-table-column label="申请金额(万元)" align="center" prop="applicationAmount" />
      <!-- <el-table-column label="申请时间" align="center" prop="applicationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applicationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="申请人" align="center" prop="empName" />
       <!-- <el-table-column label="负责人标号" align="center" prop="empId" /> -->
      <!-- <el-table-column label="拨款人" align="center" prop="appropriator" /> -->
      <el-table-column label="拨款日期" align="center" prop="appropriationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appropriationDate, '{y}-{m}-{d}') }}</span>
          {{scope.row.appropriationDate==null?'暂无拨款日期':''}}
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="state" >
       <template slot-scope="scope">
          <el-tag    v-if="scope.row.state === 0">未审核</el-tag>
          <el-tag type="warning" v-if="scope.row.state === 1">审核中</el-tag>
          <el-tag type="success" v-if="scope.row.state === 2">审核通过</el-tag>
          <el-tag type="danger" v-if="scope.row.state === 3">审核未通过</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="版本号" align="center" prop="version" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          审核状态_头-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleCheck(scope.row)"
            v-hasPermi="['official:money:state']"
          >审核</el-button>
<!--          审核状态_尾-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['official:money:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['official:money:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--    审核对话框-->
    <el-dialog :title="title" :visible.sync="stateOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="empId">
          <!--          下拉框-->
          <!--          <el-input v-model="form.empId" placeholder="请输入员工编号" />-->
          <el-select v-model="form.empId" placeholder="请选择" >
            <el-option
              v-for="item in employee"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="申请类别" prop="applyforType">
          <el-input v-model="form.applyforType" placeholder="请输入申请类别" />
        </el-form-item>
                
      <el-form-item label="审批内容" prop="content">
        <el-input v-model="form.content" type="textarea" placeholder="请输入审批内容"
          :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
      </el-form-item>
        <!-- <el-form-item label="审批内容" prop="content" type="textarea">
          <el-input v-model="form.content" placeholder="请输入审批内容" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitStateForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改财务用款管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="entryName">
          <el-input v-model="form.entryName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="申请金额(万元)" prop="applicationAmount">
          <el-input v-model="form.applicationAmount" :disabled="verification1" placeholder="请输入申请金额" />
        </el-form-item>
        <!-- <el-form-item label="申请部门" prop="depId">
          <el-input v-model="form.depId" placeholder="请输入申请部门" />
        </el-form-item> -->

<!--        部门编号改下拉框-->
        <el-form-item label="申请部门" prop="depId">
          <!-- <el-select v-model="form.depId" placeholder="请选择">
            <el-option
              v-for="item in department"
              :key="item.depId"
              :label="item.depName"
              :value="item.depId">
            </el-option>
          </el-select> -->
          <el-select v-model="form.depId" placeholder="请选择">
            <el-option
              v-for="item in department"
              :key="item.depId"
              :label="item.depName"
              :value="item.depId">
            </el-option>
          </el-select>
        </el-form-item>
        
        <!-- <el-form-item label="负责人" prop="empId">
          <el-input v-model="form.empId" placeholder="请输入负责人" />
        </el-form-item> -->

<!--        员工编号改下拉框-->
        <el-form-item label="负责人" prop="empId">
<!--          <el-input v-model="form.empId" placeholder="请输入员工编号" />-->
          <el-select v-model="form.empId" :disabled="verification1" placeholder="请选择">
            <el-option
              v-for="item in employee"
              :key="item.empId"
              :label="item.empName"
              :value="item.empId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间" prop="applicationDate">
          <el-date-picker clearable
            v-model="form.applicationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="拨款人" prop="appropriator">
          <el-input v-model="form.appropriator" placeholder="请输入拨款人" />
        </el-form-item> -->
        <!-- <el-form-item label="拨款日期" prop="appropriationDate">
          <el-date-picker clearable
            v-model="form.appropriationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择拨款日期">
          </el-date-picker>
        </el-form-item> -->

<!-- <el-form-item label="审核情况" prop="state"> -->
          <!-- <el-input v-model="form.audit" placeholder="请输入审核情况" /> -->
           <!-- <el-select v-model="form.state" placeholder="请选择"
          clearable
          @keyup.enter.native="handleQuery">
            <el-option
              v-for="item in audits"
              :key="item.state"
              :label="item.auditName"
              :value="item.state">
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入版本号" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>


<script>
import {
  listMoney,
  getMoney,
  delMoney,
  addMoney,
  updateMoney,
  listEmployee,
  listDepartment
} from "@/api/official/money";
//import {listEmployee} from "@/api/personnel/employee";
//import { listDepartment } from "@/api/personnel/department";
//调取新增审核接口
import { addApprove } from "@/api/audit/approve";
import { log } from 'util';

export default {
  verification1:true,
  name: "Money",
  data() {
    return {
      department: {},
      employee: [],
      // departmentList: [],
      // 是否显示审核弹出层
      stateOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 财务用款管理表格数据
      moneyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        entryName: null,
        depId: null,
        applicationAmount: null,
        applicationDate: null,
        empId: null,
        appropriator: null,
        appropriationDate: null,
        state: null,
        version: null
      },
      //  audits: [
      // { auditStatus: 1, auditName: "审核中" },
      // { auditStatus: 2, auditName: "通过" },
      // { auditStatus: 3, auditName: "未通过" }
      // ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        entryName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        depId: [
          { required: true, message: "申请部门不能为空", trigger: "blur" }
        ],
        empId: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        applyforType: [
          { required: true, message: "申请类别不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "审核内容不能为空", trigger: "blur" }
        ],
        applicationAmount: [
           { required: true, message: "申请金额不能为空", trigger: "blur" },
          // { type: "number", message: "申请金额必须为数字", required: true },
          {
            pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
            message: "申请金额必须为数字"
          }
        ],
        appropriator: [
          { required: true, message: "拨款人不能为空", trigger: "blur" },
          {
            validator: function(rule, value, callback) {
              //校验中文的正则：/^[\u4e00-\u9fa5]{0,}$/
              // ^[a-zA-Z\u4e00-\u9fa5]+$
              if (/^[a-zA-Z\u4e00-\u9fa5]+$/.test(value) == false) {
                callback(new Error("请输入中文或英文"));
              } else {
                //校验通过
                callback();
              }
            },
            trigger: "change"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    //发送请求
    listEmployee().then(response => {
      this.employee = response.rows;
      this.loading = false;
    });
    // 发送请求部门名称
    listDepartment().then(response => {
      this.department = response.rows;
      // this.departmentList = response.rows;
      console.log(departmentList);
      
      this.loading = false;
    });
  },
  methods: {
    /** 查询财务用款管理列表 */
    getList() {
      this.loading = true;
      listMoney(this.queryParams).then(response => {
        this.moneyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.stateOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        spendMoneyId: null,
        entryName: null,
        depId: null,
        applicationAmount: null,
        applicationDate: null,
        empId: null,
        appropriator: null,
        appropriationDate: null,
        createTime: null,
        updateTime: null,
        state: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.spendMoneyId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.verification1 = false;
      this.reset();
      this.open = true;
      this.title = "添加财务用款管理";
    },
    /** 提交审核按钮操作 */
    handleCheck(row) {
      if (row.state == 1) {
        this.$modal.confirm("已在审核中");
      } else if (row.state == 2) {
        this.$modal.confirm("审核已通过");
      } else {
        //表单重置
        this.reset();
        //修改弹窗
        const spendMoneyId = row.spendMoneyId || this.ids;
        getMoney(spendMoneyId).then(response => {
          this.remark = "money/" + spendMoneyId;
          this.form = response.data;
          this.stateOpen = true;
          this.title = "审核财务用款管理";
        });
      }
    },
    /** 提交审核数据 */
    submitStateForm() {
      //addApprove
      this.form.remark = this.remark;
      this.form.state = 1;
      //数据校验
      this.$refs["form"].validate(valid => {
        if (valid) {
          //验证数据不为空操作

          addApprove(this.form).then(response => {
            this.$modal.msgSuccess("新增审核成功");
            this.stateOpen = false;
            //this.getList();
          }),
            //
            //
            updateMoney(this.form).then(response => {
              //this.$modal.msgSuccess("修改成功");
              this.stateOpen = false;
              this.getList();
            });
        }
      });
    },
    /** 提交审核 */
    // handleCheck(){
    //   //表单重置
    //   this.reset();
    //   this.stateOpen = true;
    //   this.title = "添加审核管理";

    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.verification1 = true;
      this.reset();
      const spendMoneyId = row.spendMoneyId || this.ids;
      getMoney(spendMoneyId).then(response => {
        console.log(response.data);
        this.form = response.data;
        this.open = true;
        this.title = "修改财务用款管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.spendMoneyId != null) {
            updateMoney(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMoney(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const spendMoneyIds = row.spendMoneyId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除财务用款管理编号为"' + spendMoneyIds + '"的数据项？'
        )
        .then(function() {
          return delMoney(spendMoneyIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "official/money/export",
        {
          ...this.queryParams
        },
        `money_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
